package com.blog.utils;

import com.blog.Repository.ArticleSearchRepository;
import com.blog.dto.ArticleDTO;
import com.blog.dto.EsArticleDTO;
import com.blog.entity.Article;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Component;

import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class ArticleSearchUtils {
    @Autowired
    private ArticleSearchRepository articleSearchRepository;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    // 添加文章到es里面
    public void saveArticle(EsArticleDTO esArticleDTO)
    {
        esArticleDTO.initSuggest();
        System.out.println(esArticleDTO.getCreateTime());
        // 直接使用DTO对象索引
        IndexQuery indexQuery = new IndexQueryBuilder()
                .withId(esArticleDTO.getId().toString())
                .withObject(esArticleDTO)
                .build();

        elasticsearchRestTemplate.index(indexQuery, IndexCoordinates.of("blog"));
    }
//    public void saveArticles(List<Article> articles)
//    {
//        articleSearchRepository.saveAll(articles);
//    }
//    // 根据id删除文章
//    public void deleteArticle(Integer id)
//    {
//        articleSearchRepository.deleteById(id);
//    }
//    // 根据关键字搜索文章
//    public List<Article> searchByTitleOrContent(String keyword)
//    {
//        return articleSearchRepository.findByTitleAndContent(keyword,keyword);
//    }
//    public List<Article> advancedSearch(String keyword) {
//        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
//                .withQuery(QueryBuilders.multiMatchQuery(keyword, "title", "content"))
//                .build();
//        SearchHits<Article> searchHits = elasticsearchRestTemplate.search(searchQuery, Article.class);
//        return searchHits.stream()
//                .map(hit->hit.getContent())
//                .collect(Collectors.toList());
//    }
//    public List<Article> searchWithPagination(String keyword, int page, int size) {
//        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
//                .withQuery(QueryBuilders.multiMatchQuery(keyword, "title", "content"))
//                .withPageable(PageRequest.of(page,size))
//                .build();
//        SearchHits<Article> searchHits = elasticsearchRestTemplate.search(searchQuery,Article.class);
//        return searchHits.stream()
//                .map(hit->hit.getContent())
//                .collect(Collectors.toList());
//    }
//    public boolean isIndexEmpty() {
//        long count = articleSearchRepository.count();
//        return count == 0;
//    }
}
